//>>=======================================================//
// AUIPC DEMONSTRATION PROGRAM                             //
//                                                         //
// Assume the program begins at address 0x00000000.        //
//                                                         //
// The first AUIPC instruction creates the address 0x1000. //
// Five integers are stored beginning at that address.     //
//                                                         //
// Values= 10, 20, 30, 40, 50                              //
// Expected sum= 150                                       // 
//=========================================================//





//|>  AUIPC DEMONSTRATION
// ----------------------------------------------------------
// Create a PC-relative memory address
// ----------------------------------------------------------
start:

auipc x5, 0x1          // x5 = PC + 0x1000
                       // If PC = 0x0000, x5 = 0x1000

addi  x6, x5, 0        //  x6 = current array address

// ----------------------------------------------------------
//|> Store five integers in memory
// ----------------------------------------------------------
addi  x7, x0, 10       //  x7 = 10
sw    x7, 0(x6)        //|>  memory[0x1000] = 10

addi  x7, x0, 20       // x7 = 20
sw    x7, 4(x6)        //|>  memory[0x1004] = 20

addi  x7, x0, 30       // x7 = 30
sw    x7, 8(x6)        //|>  memory[0x1008] = 30

addi  x7, x0, 40       // x7 = 40
sw    x7, 12(x6)       //|>  memory[0x100C] = 40

addi  x7, x0, 50       // x7 = 50
sw    x7, 16(x6)       //|>  memory[0x1010] = 50

// ----------------------------------------------------------
//|> Prepare to process the array
// ----------------------------------------------------------

addi  x6, x5, 0        //<|  x6 = address of first integer
addi  x8, x0, 5        //<|  x8 = number of integers
addi  x9, x0, 0        //<|  x9 = sum
addi  x10, x0, 0       //<|  x10 = loop counter

// ----------------------------------------------------------
//|>  Add the integers
// ----------------------------------------------------------

LOOP:
                      
lw    x11, 0(x6)       //<|>   Add a memory value
add   x9, x9, x11      //     Add x11 to the sum

addi  x6, x6, 4        // Move to the next word
addi  x10, x10, 1      //<|> Increment loop counter

blt   x10, x8, LOOP    // Count == 5 ? 

// ----------------------------------------------------------
//|>  Display the result
// ----------------------------------------------------------

cout << "Array address = " << _to_hex x5 << endl;
cout << "Number of values = " << x8 << endl;
cout << "Sum = " << x9 << endl;

// ----------------------------------------------------------
// Capture the address of another instruction
// ----------------------------------------------------------

auipc x12, 0           //<|> x12 = address of this instruction

cout << "Current program address = ";
cout << _to_hex x12 << endl;

// ----------------------------------------------------------
// Create another PC-relative address
// ----------------------------------------------------------

auipc x13, 0x2         // x13 = current PC + 0x2000

cout << "PC plus 0x2000 = ";
cout << _to_hex x13 << endl;

// ----------------------------------------------------------
// Program finished
// ----------------------------------------------------------

END:   //<


